package Other.GreaterTree;


//538. 把二叉搜索树转换为累加树
public class GreatTree {
    
}

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }


  //二叉排序树中序遍历是由小到大,反中序遍历是由大到小,那么把所有值的和记录下来,直接累加
class Solution {

    private int sum =0;
    public TreeNode convertBST(TreeNode root) {
        if(root==null)return null;
        TreeNode r = convertBST(root.right);
        sum+=root.val;
        TreeNode rooTreeNode = new TreeNode(sum);
        TreeNode l = convertBST(root.left);
        rooTreeNode.left=l;
        rooTreeNode.right=r;
        return rooTreeNode;
    }
}